<?=view('header.html')?>

<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">
            <div class="layui-card">

                <div class="layui-card-header">
                    角色管理
                </div>

                <div class="layui-card-body">

                    <table id="listTable" lay-filter="listTable"></table>
                    <script type="text/html" id="toolbarOplog">
                    <div class="layui-btn-container">
                        <button type="button" class="layui-btn layui-btn-sm" lay-event="addRole">创建角色</button>
                        <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delBatch">删除</button>
                    </div>
                </script>

                    <script type="text/html" id="statusTpl">
                    <input type="checkbox" name="status" value="{{d.status}}" lay-skin="switch" lay-text="已启用|已禁用" lay-filter="statusSwitch" {{ d.status== 1 ? 'checked' : '' }}>
                </script>

                    <script type="text/html" id="barOplog">
                    {{# if(d.id!=1){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-default" lay-event="auth">权限设置</a>
                    {{# } }}
                    <a class="layui-btn layui-btn-xs layui-btn-default" lay-event="addUser">添加管理员</a>
                    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">修改角色</a>
                    {{# if(d.id!=1){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除角色及管理员</a>
                    {{# } }}
                </script>

                </div>
            </div>
        </div>
    </div>
    <?=view('footer.html')?>
    <script type="text/javascript">
        //JavaScript代码区域
        layui.use(['layer', 'table', 'form', 'jquery', 'element', 'laydate'], function () {
            var element = layui.element
                , $ = layui.jquery
                , table = layui.table
                , form = layui.form
                , layer = layui.layer
                , laydate = layui.laydate;
            


            //数据初始化
            var tableInit = table.render({
                elem: '#listTable' //指定原始表格元素选择器（推荐id选择器）
                , title: '后台管理员操作记录'
                , url: adminpath + "admin/rbac/role"
                , method: 'post'
                , toolbar: '#toolbarOplog'
                , cols: [[
                    { type: 'checkbox' },
                    { field: 'id', title: 'ID', width: 80, unresize: true, sort: true },
                    { field: 'name', title: '角色名称', width: 200 },
                    { field: 'status', title: '是否启用', templet: '#statusTpl', unresize: true },
                    { fixed: 'right', title: '操作', toolbar: '#barOplog' }
                ]]
                , page: {
                    layout: ['prev', 'page', 'next', 'count']
                    , groups: 5
                    , first: true
                    , last: true
                }
                , response: {
                    statusCode: 0,
                    countName: 'count'
                }
                , parseData: function (res) {
                    return {
                        'code': res.code,
                        "data": res.data.data,
                        'count': res.data.total
                    };
                }
            });

            //批量事件
            table.on('toolbar(listTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);

                switch (obj.event) {
                    case 'delBatch':
                        var data = checkStatus.data;
                        var ids = [];
                        data.forEach(function (n, i) {
                            ids.push(n.id);
                        });
                        if (ids != '') {
                            layer.confirm('确定要删除吗？', { icon: 3, title: '消息提示' }, function (index) {
                                $.post(adminpath + 'admin/rbac/delRole', { id: ids }, function (res) {
                                    if (res.code == 1) {
                                        layer.msg('操作成功，正在刷新...', { icon: 1, time: 1500 });
                                    } else {
                                        layer.msg(res.msg, { icon: 5, time: 2000, anim: 6 });
                                    }
                                    tableInit.reload();
                                }, 'json')
                                layer.close(index);
                            });

                        } else {
                            layer.msg('请选择要操作的数据', { icon: 5, anim: 6 });
                            return false;
                        }
                        break;

                    case 'addRole':
                        layer.open({
                            title: '创建角色',
                            type: 2,
                            area: ['600px', '400px'],
                            anim: 2,
                            shadeClose: false,
                            content: adminpath + "admin/rbac/addRole"
                        });
                        break;
                }
            });

            //监听行事件
            table.on('tool(listTable)', function (obj) {
                var data = obj.data;
                var id = data.id;

                switch (obj.event) {
                    //权限设置
                    case 'auth':
                        layer.open({
                            title: '设置角色(' + data.name + ')权限',
                            type: 2,
                            area: ['600px', '90%'],
                            anim: 2,
                            shadeClose: false,
                            content: adminpath + 'admin/rbac/authRole?roleid=' + id
                        });
                        break;
                    case 'addUser':
                        layer.open({
                            title: '添加角色',
                            type: 2,
                            area: ['500px', '90%'],
                            anim: 2,
                            shadeClose: false,
                            skin: 'layui-layer-molv',
                            content: adminpath + 'admin/rbac/add?roleid=' + id
                        });
                        break;

                    case 'edit':
                        layer.open({
                            title: '修改角色',
                            type: 2,
                            area: ['500px', '300px'],
                            anim: 2,
                            shadeClose: false,
                            skin: 'layui-layer-molv',
                            content: adminpath + 'admin/rbac/editRole?id=' + id
                        });
                        break;

                    case 'del':
                        layer.confirm('删除角色会删除该角色里的所有用户', function (index) {
                            $.post(adminpath + 'admin/rbac/delRole', { id: id }, function (res) {
                                if (res.code == 1) {
                                    obj.del();
                                    layer.msg('操作成功', { icon: 1, time: 1500 });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6 });
                                }
                                tableInit.reload();
                            }, 'json');
                            layer.close(index);
                        });
                        break;
                }
            });


            //监听状态操作
            form.on('switch(statusSwitch)', function (obj) {
                var id = obj.othis.parents('tr').find("td:eq(1)").find(".layui-table-cell").text();
                var title = '';
                var val = '';
                if (this.value == 1) {
                    title = '您确定要[禁用]它吗?';
                    val = 0;
                } else {
                    title = '您确定要[启用]它吗?';
                    val = 1;
                }

                //点确认再切换
                var x = obj.elem.checked;
                obj.elem.checked = !x;
                form.render('checkbox');

                layer.confirm(title, function (index) {
                    $.post(adminpath + "admin/rbac/roleStatus", { id: id, value: val }, function (res) {
                        if (res.code == 0) {
                            layer.msg('操作成功', { icon: 1, time: 1500 });
                        } else {
                            layer.msg(res.msg, { icon: 5, anim: 6 });
                        }
                        tableInit.reload();
                    }, 'json');
                    layer.close(index);
                });
            });


            //搜索
            form.on('submit(ams_search)', function (data) {
                var field = data.field;
                tableInit.reload({
                    where: {
                        username: field.username
                        , day: field.day
                    }
                    , page: {
                        curr: 1
                    }
                });
            });

            //搜索重置
            form.on('submit(ams_reset)', function (data) {
                $("#search")[0].reset();
                var field = data.field;
                $.each(field, function (k, v) {
                    field[k] = '';
                });
                tableInit.reload({
                    where: {
                        username: field.username
                        , day: field.day
                    }
                    , page: {
                        curr: 1
                    }
                });
            });

            //添加
            $("#bindAdd").on('click', function () {
                layer.open({
                    title: '添加角色',
                    type: 2,
                    area: ['500px', '300px'],
                    anim: 2,
                    shadeClose: false,
                    skin: 'layui-layer-molv',
                    content: adminpath + "admin/rbac/addRole"
                });
            });


        });
    </script>
</body>

</html>
